package main;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author WIN7
 */
public class StoryModeMenuOptionScreen extends GameCanvas implements Runnable, CommandListener{
    private MainMidlet midlet;
    private Item item;
    private Character character;
    private String[] menuString;
    private boolean isCity = true;
    private Sprite[] spriteMenuPanel;
    private Sprite spriteCharacter;
    private int currentMenu = 0;
    private boolean running = true;
    private Command cmd = new Command("Back", Command.BACK, 0);
    public StoryModeMenuOptionScreen(MainMidlet midlet, Item item, Character character)
    {
        super(true);
        setFullScreenMode(true);
        this.midlet = midlet;
        this.item = item;
        this.character = character;
        if(item.getName().indexOf("City") != -1)
            isCity = true;
        else
            isCity = false;
        if(isCity)
        {
            String[] menu = 
            {
                "Go to gym",
                "Go to hospital",
                "Go to market"
            };
            menuString = menu;
        }else
        {
            String[] menu = 
            {
                "Wild Pokemon Battle",
            };
            menuString = menu;
        }
        spriteCharacter = new Sprite(character.getSprite());
        spriteCharacter.setPosition(getWidth()/2 - 10, getHeight()/5);
        item.getSprite().setPosition(getWidth()/2, getHeight()/5 + 20);
        spriteMenuPanel = new Sprite[menuString.length];
        try
        {
            for(int i=0; i<menuString.length; i++)
                spriteMenuPanel[i] = new Sprite(Image.createImage("/MenuPanel.png"));
        }catch(Exception ex)
        {
            ex.printStackTrace();
            return;
        }
        this.addCommand(cmd);
        this.setCommandListener(this);
    }
    public void stop()
    {
        running = false;
    }
    
    public void start()
    {
        Thread thread = new Thread(this);
        running = true;
        thread.start();
    }
    private void chooseMenu()
    {
        switch(currentMenu)
        {
            case 0:
                stop();
                midlet.showPlayingCanvas(character, new Pokemon("Pidgey.png", "/Pidgey.png", 30, 30, 4, 4, 2, 4), item, false);
                break;
            case 1:
                stop();
                midlet.showHealPokemonScreen(character, item);
                break;
            case 2:
                break;
            case 3:
                break;
            default:
                break;
        }
    }
    private void updateState()
    {
        int keyState = getKeyStates();
        if((keyState & UP_PRESSED) != 0)
        {
            currentMenu --;
            if(currentMenu < 0)
                currentMenu = menuString.length - 1;
        }else if((keyState & DOWN_PRESSED) != 0)
        {
            currentMenu = (currentMenu + 1)%menuString.length;
        }else if((keyState & FIRE_PRESSED) != 0)
        {
            chooseMenu();
        }
        spriteCharacter.nextFrame();
        character.animation();
    }
    
    public void paint(Graphics g)
    {
        //fill background
        g.setColor(0, 0, 255);
        g.fillRect(0, 0, getWidth(), getHeight());
        Font font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_LARGE);
        g.setFont(font);
        g.setColor(255, 255, 0);
        String nameItem = item.getName();
        //draw Item name
        g.drawString(nameItem, getWidth()/2, 20, Graphics.BASELINE|Graphics.HCENTER);
        //draw character
        spriteCharacter.paint(g);
        //draw character's pokemon
        character.getOwnPokemon().paint(g, spriteCharacter.getX() + 20, spriteCharacter.getY(), true, false);
        //draw item
        item.getSprite().paint(g);
        //draw menu panel
        for(int i=0; i<menuString.length; i++)
        {
            spriteMenuPanel[i].setPosition((getWidth() - spriteMenuPanel[i].getWidth())/2, 
                    character.getY() + (int)(spriteMenuPanel[i].getHeight()*(i + 1)*1.5));
            spriteMenuPanel[i].paint(g);
        }
        //draw text on menu panel
        font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        g.setFont(font);
        for(int i=0; i<menuString.length; i++)
        {
            if(i == currentMenu)
                g.setColor(255, 0, 0);
            else
                g.setColor(255, 0, 255);
            g.drawString(menuString[i], getWidth()/2, spriteMenuPanel[i].getY() + 25, Graphics.BASELINE|Graphics.HCENTER);
        }
        
        flushGraphics();
    }
    
    public void run() {
        Graphics g = getGraphics();
        while(running)
        {
            updateState();
            paint(g);
            repaint();
            try
            {
                Thread.sleep(150);
            }catch(Exception ex)
            {
                ex.printStackTrace();
                return;
            }
        }
    }

    public void commandAction(Command c, Displayable d) {
        if(c == cmd)
        {
            stop();
            midlet.showStoryModeMenu();
        }
    }
    
}
